' This application lets you create and modify your Windows calendars, as well as other
 '* iCal calendar files, by changing your appointments and adding new appointments. You
 '* can also import and export your appointments from/to the current calendar or other
 '* calendars.
 '


Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.Calendar

Namespace C4F.DevKit.Calendar.Samples
	Public Partial Class MainForm
		Inherits Form
		Private _selectDialog As SelectAppointmentDialog

		Public Sub New()
			InitializeComponent()
		End Sub

		Public Sub RefreshAppointmentList()
			_selectDialog.RefreshList()
		End Sub

		Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles newToolStripMenuItem.Click, newWindowToolStripMenuItem.Click, newToolStripButton.Click
			CreateCalendar()
		End Sub

		Private Sub OnOpen(ByVal sender As Object, ByVal e As EventArgs) Handles openToolStripMenuItem.Click, openToolStripButton.Click
			OpenCalendar()
		End Sub

		Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles exitToolStripMenuItem.Click
			Me.Close()
		End Sub

		Private Sub CascadeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cascadeToolStripMenuItem.Click
			LayoutMdi(MdiLayout.Cascade)
		End Sub

		Private Sub TileVerticalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tileVerticalToolStripMenuItem.Click
			LayoutMdi(MdiLayout.TileVertical)
		End Sub

		Private Sub TileHorizontalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tileHorizontalToolStripMenuItem.Click
			LayoutMdi(MdiLayout.TileHorizontal)
		End Sub

		Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles arrangeIconsToolStripMenuItem.Click
			LayoutMdi(MdiLayout.ArrangeIcons)
		End Sub

		Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles closeAllToolStripMenuItem.Click
			For Each childForm As Form In MdiChildren
				childForm.Close()
			Next childForm
		End Sub

		Private Sub loadCalendarToolStripLabel_ButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles loadCalendarToolStripLabel.ButtonClick
			OpenCalendar()
		End Sub

		Private Sub CreateCalendar()
			Try
				calendarService1.CreateCalendar()

				If String.IsNullOrEmpty(calendarService1.CalendarFilePath) Then
					Return
				End If

				CloseCalendar()

				_selectDialog = New SelectAppointmentDialog(Me.calendarService1)
				_selectDialog.MdiParent = Me
				_selectDialog.Show()
				toolStripStatusLabel.Text = "Created calendar: " & calendarService1.CalendarFilePath
			Catch e1 As Exception
				MessageBox.Show(String.Format("Error creating new calendar file {0}. Please choose a different file.", calendarService1.CalendarFilePath), "Error while creating new calendar")
			End Try
		End Sub

		Private Sub OpenCalendar()
			calendarService1.CalendarFilePath = calendarService1.PromptForOpenLocation()

			If String.IsNullOrEmpty(calendarService1.CalendarFilePath) Then
				Return
			End If

			CloseCalendar()

			Try
				_selectDialog = New SelectAppointmentDialog(Me.calendarService1)
				_selectDialog.MdiParent = Me
				_selectDialog.Show()
				toolStripStatusLabel.Text = "Loaded calendar: " & calendarService1.CalendarFilePath
			Catch e1 As Exception
				MessageBox.Show(String.Format("Could not open calendar {0}. Please choose a different file", calendarService1.CalendarFilePath), "Error while trying to open calendar")
			End Try
		End Sub

		Private Sub CloseCalendar()
			For Each childForm As Form In Me.MdiChildren
				childForm.Close()
			Next childForm

			toolStripStatusLabel.Text = ""
		End Sub

		Private Sub newCalendarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles newCalendarToolStripMenuItem.Click
			CreateCalendar()
		End Sub
	End Class
End Namespace
